home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fritz: All Fritz
/
All Fritz.zip
/
All Fritz
/
FILES
/
PROGMISC
/
FORTRAN1.LZH
/
GAUSS.FOR
< prev
next >
Wrap
Text File
|
1988-02-08
|
7KB
|
265 lines
SUBROUTINE GAUSS ( A, Y, COEF, N, ERROR )
C*
C* *******************************
C* *******************************
C* ** **
C* ** GAUSS **
C* ** **
C* *******************************
C* *******************************
C*
C* SUBPROGRAM :
C* GAUSSIAN ELIMINATION
C*
C* AUTHOR :
C* ART RAGOSTA
C* MS 207-5
C* AMES RESEARCH CENTER
C* MOFFETT FIELD, CALIF 94035
C* (415)694-5578
C*
C* PURPOSE :
C* TO SOLVE A SET OF SIMULTANEOUS, LINEAR EQUATIONS.
C* THE FORM OF THE EQUATIONS IS :
C* Y1 = A1,1*X1 + A1,2*X2 + ... A1,N*XN
C* Y2 = A2,1*X1...
C* .
C* .
C* YN = AN,1*X1... AN,N*XN
C*
C* THE SOLUTION IS OF THE FORM :
C* X1 = COEF(1)
C* X2 = COEF(2)
C* .
C* .
C* XN = COEF(N)
C*
C* REFERENCE : "PASCAL PROGRAMS FOR ENGINEERS AND SCIENTISTS",
C* BY ALAN R. MILLER, 1981, SYBEX INC.
C*
C* INPUT ARGUMENTS :
C* A = N*N INPUT MATRIX(DESTROYED)
C* Y = INPUT VECTOR OF LENGTH, N
C* N = NUMBER OF EQUATIONS
C*
C* OUTPUT ARGUMENTS :
C* COEF = SOLUTION VECTOR OF LENGTH, N
C* ERROR = BOOLEAN ERROR FLAG (MATRIX SINGULAR IF TRUE)
C*
C* INTERNAL WORK AREAS :
C* NONE
C*
C* COMMON BLOCKS :
C* NONE
C*
C* FILE REFERENCES :
C* NONE
C*
C* SUBPROGRAM REFERENCES :
C* NONE
C*
C* ERROR PROCESSING :
C* IF A ZERO APPEARS ON THE DIAGONAL AND CAN'T BE REMOVED, THE
C* MATRIX IS SINGULAR.
C*
C* TRANSPORTABILITY LIMITATIONS :
C* NONE
C*
C* ASSUMPTIONS AND RESTRICTIONS :
C* NONE
C*
C* LANGUAGE AND COMPILER :
C* ANSI FORTRAN 77
C*
C* VERSION AND DATE :
C* VERSION I.0 5-MAR-85
C*
C* CHANGE HISTORY :
C* 5-MAR-85 INITIAL VERSION
C*
C***********************************************************************
C*
DIMENSION A(N,N), Y(N), COEF(N)
LOGICAL ERROR
C
ERROR = .FALSE.
N1 = N - 1
DO 50 I = 1, N1
AMAX=ABS(A(I,I))
L = I
I1 = I+1
C
C ----- FIND LARGEST ELEMENT IN THIS COLUMN
C
DO 10 J = I1, N
IF (ABS(A(J,I)) .GT. AMAX)THEN
AMAX = ABS(A(J,I))
L = J
ENDIF
10 CONTINUE
C
C ----- IF THE LARGEST ELEMENT IS ZERO, THE MATRIX IS SINGULAR
C
IF (AMAX .EQ. 0.0)THEN
ERROR = .TRUE.
RETURN
ELSE
C
C -------- IF THE LARGEST ELEMENT IS NOT ALREADY ON THE DIAGONAL, PUT IT
C -------- THERE BY SWAPPING ROWS
C
IF (L .NE. I) THEN
DO 20 J = 1, N
TEMP = A(L,J)
A(L,J) = A(I,J)
A(I,J) = TEMP
20 CONTINUE
TEMP = Y(L)
Y(L) = Y(I)
Y(I) = TEMP
ENDIF
C
C -------- DIVIDE EACH ELEMENT IN ROW BY THE LARGEST
C
DO 40 J = I1, N
TEMP = A(J,I)/A(I,I)
C
C -------- NOW SUBTRACT THIS ROW FROM EACH SUBSEQUENT ROW
C
DO 30 K = I1, N
A(J,K) = A(J,K) - TEMP*A(I,K)
30 CONTINUE
Y(J) = Y(J) - TEMP*Y(I)
40 CONTINUE
ENDIF
50 CONTINUE
C
C --- IF A ZERO IS LEFT ON THE DIAGONAL, IT'S SINGULAR
C
IF (A(N,N) .EQ. 0.0)THEN
ERROR = .TRUE.
C
C --- SUBSTITUTE FOR SOLUTION
C
ELSE
COEF(N) = Y(N)/A(N,N)
I = N1
60 SUM = 0.0
I1 = I + 1
DO 70 J = I1, N
SUM = SUM + A(I,J)*COEF(J)
70 CONTINUE
COEF(I) = (Y(I)-SUM)/A(I,I)
I = I - 1
IF (I .GT. 0)GO TO 60
ENDIF
RETURN
END
C
C---END GAUSS
C
CHARACTER FUNCTION GETC ( NIN )
C*
C* *******************************
C* *******************************
C* ** **
C* ** GETC **
C* ** **
C* *******************************
C* *******************************
C*
C* SUBPROGRAM :
C* GET CHARACTER
C*
C* AUTHOR :
C* ART RAGOSTA
C* MS 207-5
C* AMES RESEARCH CENTER
C* MOFFETT FIELD, CA 94035
C* (415) 694-5578
C*
C* PURPOSE :
C* GET A SINGLE CHARACTER FROM THE INPUT UNIT...
C* TAKE CARE OF NEW LINES AND END-OF-FILE
C*
C* INPUT ARGUMENTS :
C* NIN - INPUT UNIT NUMBER
C*
C* OUTPUT ARGUMENTS :
C* GETC - THE NEXT CHARACTER (FUNCTION VALUE)
C* NOTE: CHAR(26) IS RETURNED FOR ENDFILE, CHAR(13) FOR ENDLINE
C*
C* INTERNAL WORK AREAS :
C* NONE
C*
C* COMMON BLOCKS :
C* NONE
C*
C* FILE REFERENCES :
C* NIN
C*
C* SUBPROGRAM REFERENCES :
C* MERROR, LENGTH
C*
C* ERROR PROCESSING :
C* NONE
C*
C* TRANSPORTABILITY LIMITATIONS :
C* THE SAVE STATEMENT (WHICH IS COMMENTED) MAY HAVE TO BE INCLUDED
C* ON SOME SYSTEMS.
C*
C* ASSUMPTIONS AND RESTRICTIONS :
C* ALL TRAILING BLANKS ARE STRIPPED OFF
C* INPUT LINE LENGTH IS RESTRICTED TO 133 CHARACTERS
C*
C* LANGUAGE AND COMPILER :
C* ANSI FORTRAN 77
C*
C* VERSION AND DATE :
C* VERSION I.0 10-SEP-85
C*
C* CHANGE HISTORY :
C* 10-SEP-85 INITIAL VERSION
C*
C***********************************************************************
C*
PARAMETER (LMAX=133,NSTART=LMAX+1)
CHARACTER *(LMAX) LINE
LOGICAL EOF
C SAVE NPTR, LPTR, EOF, LINE
DATA NPTR/NSTART/, LPTR/LMAX/
DATA EOF/.FALSE./
C
C --- END OF LINE WAS REACHED ON LAST ENTRY... GET NEW LINE
C
IF (NPTR .GT. LPTR) THEN
IF ( EOF ) THEN
CALL MERROR('Attempted get after end of file.')
STOP
ENDIF
READ(NIN,900,END=1000) LINE
LPTR = LENGTH(LINE)+1
NPTR = 1
ENDIF
C
C --- RETURN NEXT CHARACTER UNLESS THERE ARE NO MORE... THEN RETURN <CR>
C
IF (NPTR .EQ. LPTR) THEN
GETC = CHAR(13)
ELSE
GETC = LINE(NPTR:NPTR)
ENDIF
NPTR = NPTR + 1
RETURN
C
C --- END OF FILE
C
1000 GETC = CHAR(26)
EOF = .TRUE.
RETURN
900 FORMAT(A)
END
C
C---END GETC
C